package com.tuanbiao.dialog;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.tuanbiao.R;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.keyevent.listener.OnBlastKeyDownListener;
import com.tuanbiao.keyevent.listener.OnBlastNettedKeyDownListener;
import com.tuanbiao.utils.ClickUtil;

import timber.log.Timber;

public class ErrorDialog extends CenterPopupView {

    private String errorMsg = "";
    private OnErrorMenuItemClickListener listener;
    private boolean isChargeDialog = false;

    public ErrorDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_error_layout;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        TextView textView = findViewById(R.id.dialogErrorText);
        if(isChargeDialog){
            findViewById(R.id.dialogErrorNextDivider).setVisibility(View.GONE);
            findViewById(R.id.dialogErrorNext).setVisibility(View.GONE);
            String msg = errorMsg +"\n提示:如需继续起爆，请同时按下F1+F4。";
            textView.setText(msg);
        } else {
            String msg = errorMsg +",按F4继续。";
            textView.setText(msg);
        }

        findViewById(R.id.dialogErrorLook).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onLookErrorListClick();
            }
        });
        findViewById(R.id.dialogErrorExit).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                listener.onExitClick();
            }
        });
        findViewById(R.id.dialogErrorNext).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                listener.onNextClick();
            }
        });

    }


    @Override
    protected boolean processKeyEvent(int keyCode, KeyEvent event) {
        return onKeyDownEvent(keyCode, event);
    }

    private boolean onKeyDownEvent(int keyCode, KeyEvent event) {
        Timber.e("onKeyDown keyCode:"+keyCode);

        if(isChargeDialog){
            KeyEventHelper.getInstance().checkBlastKeyDown(keyCode, new OnBlastKeyDownListener() {
                @Override
                public void onBlast() {
                    if(ClickUtil.isFastClick()){
                        return ;
                    }
                    findViewById(R.id.dialogErrorNext).performClick();
                }

                @Override
                public void onCheckErrorCaps() {
                    if(ClickUtil.isFastClick()){
                        return ;
                    }
                    findViewById(R.id.dialogErrorLook).performClick();
                }
            });
        } else {
            if(ClickUtil.isFastClick()){
                return true;
            }
            KeyEventHelper.getInstance().checkBlastNettedKeyDown(keyCode, new OnBlastNettedKeyDownListener() {
                @Override
                public void onCharge() {
                    findViewById(R.id.dialogErrorNext).performClick();
                }

                @Override
                public void onCheckErrorCaps() {
                    findViewById(R.id.dialogErrorLook).performClick();
                }
            });

        }
        return true;
    }

    public interface OnErrorMenuItemClickListener{
        void onLookErrorListClick();
        void onExitClick();
        void onNextClick();
    }


    public static void show(Context context,String msg,boolean isChargeDialog,OnErrorMenuItemClickListener l){
        ErrorDialog pop = new ErrorDialog(context);
        pop.listener = l;
        pop.errorMsg = msg;
        pop.isChargeDialog = isChargeDialog;
       new XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(pop)
                .show();
    }
}
